home *** CD-ROM | disk | FTP | other *** search
/ Collection of Tools & Utilities / Collection of Tools and Utilities.iso / sound / rukc10.zip / X02.C < prev    next >
C/C++ Source or Header  |  1993-03-06  |  11KB  |  346 lines

  1.  
  2. #include <dos.h>
  3. #include <conio.h>
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6. #include <stdarg.h>
  7.  
  8. #include "ruckdac.h"
  9.  
  10. /*
  11. X02.c 28-Feb-93 chh
  12. Load & Play mod file
  13. */
  14.  
  15. /*
  16. The following structures are in ruckdac.h
  17. */
  18.  
  19. /*
  20. DACDATA is in DGROUP so could probably get away with a __near here dear
  21. */
  22.  
  23. extern struct DacDataArea __pascal DACDATA;
  24.  
  25. struct SysInfoPack SIP;
  26. struct InitPack IP;
  27. struct XitPack XP;
  28. struct LoadPack LP;
  29. struct SetModPack SMP;
  30. struct SetProPack SPP;
  31. struct PlaybackPack PBP;
  32.  
  33.  
  34. int rez, rez2;      /* result status codes */
  35. char nums[9] = {7}; /* number buffer for _cgets()*/
  36. char filename[81];  /* pathname to load */
  37.  
  38. /* typedef unsigned int uint; */
  39.  
  40. int pick_device(int *devID, unsigned int *HighRate)
  41. {
  42.  
  43.     /*
  44.     Just ask for device to use and return it and that device's effective
  45.     top-end rate (nothing concrete in the returned top-end...)
  46.     */
  47.  
  48.     int td=0;
  49.  
  50.     *HighRate = 0;
  51.  
  52.     SIP.Func = SysInfoDac;
  53.     rez = RUCKDAC(&SIP);
  54.     if (rez == 0) {
  55.         printf("CPU is a %u/%u\n",SIP.CPU,SIP.MHz);
  56.  
  57.         printf("\n0. End program");
  58.         printf("\n1. PC speaker at port 42h");
  59.         if (SIP.SD[1].device)
  60.             printf("\n2. LPT-DAC on LPT1, port %xh",SIP.SD[1].Port);
  61.         if (SIP.SD[2].device)
  62.             printf("\n3. Disney Sound Source port %xh",SIP.SD[2].Port);
  63.         if (SIP.SD[3].device)
  64.             printf("\n\n4. AdLib Music Synthesizer Card, port %xh",SIP.SD[3].Port);
  65.         if (SIP.SD[4].device)
  66.             printf("\n5. Sound Blaster, port %xh",SIP.SD[4].Port);
  67.         if (SIP.SD[5].device)
  68.             printf("\n6. Sound Blaster Pro, port %xh\n",SIP.SD[5].Port);
  69.  
  70.         /*
  71.         Can't play mods from XMS (well...)
  72.         BTW, if this code looks familiar, it's because it is (see X01.C)
  73.         */
  74.  
  75.         printf("\nSelection: ");
  76.         td = atoi(_cgets(nums));
  77.         td--;   /* since devices are numbered 0 to 5 */
  78.  
  79.         if ((td >=0) && (td <=5)) { /* validate device selected available */
  80.             if (SIP.SD[td].device == 0)
  81.                 td = -1;
  82.         }
  83.         else
  84.             td = -1;
  85.  
  86.         switch (td) {
  87.         case 0:                     /* 0 - PC speaker     */
  88.             *HighRate = 18000;      /* 1 - LPT-DAC        */
  89.             break;                  /* 2 - Sound Source   */
  90.         case 1:                     /* 3 - AdLib          */
  91.             *HighRate = 23000;      /* 4 - Sound Blaster  */
  92.             break;                  /* 5 - Sound Blaster Pro ((STEREO)) */
  93.         case 2:
  94.             *HighRate = 7000;
  95.             break;
  96.         case 3:
  97.             *HighRate = 12000;
  98.             break;
  99.         case 4:
  100.             *HighRate = 23000;
  101.             break;
  102.         case 5:
  103.             *HighRate = 22750;  /* ((STEREO)) 2*22750=45500Hz*/
  104.             break;
  105.         default:
  106.             td=-1;
  107.         }
  108.     }
  109.  
  110.     *devID = td;
  111.     return(rez);
  112. }
  113.  
  114.  
  115. int init_device(int devID)
  116. {
  117.     /*
  118.     Initialize RUCKDAC and device and register ExitMod with _atexit.
  119.     Mod play does not have a hi-rez mode for PC speaker & AdLib as does Dac.
  120.     */
  121.  
  122.     IP.Func = InitDac;
  123.     IP.DeviceID = devID;
  124.     IP.IOport = SIP.SD[devID].Port;
  125.     IP.IRQline = SIP.SD[devID].IRQ;
  126.     IP.DMAch = SIP.SD[devID].DMA;
  127.  
  128.     rez = RUCKDAC(&IP);                 /* Initialize */
  129.     if (rez == 0) {
  130.         XP.Func = AtExitMod;            /* Try this with based pointers */
  131.         rez2 = RUCKDAC(&XP);            /* in use...could be a C7 bug   */
  132.         if (rez2 != 0) {                /* since _atexit seems to share */
  133.                                         /* (overwrite) memory used by   */
  134.                                         /* the compiler's __based data  */
  135.                                         /* Could just be me...  `       */
  136.             printf("AtExitMod failed, press Enter to continue");
  137.             getchar();
  138.         }
  139.     }
  140.     return(rez);
  141. }
  142.  
  143.  
  144. int main()
  145. {
  146.  
  147.     int devID=-1;
  148.     unsigned int HighRate=5000, SampleRate=5000;
  149.  
  150.     printf("X02.C - RUCKUS-DAC play of MOD file example. [930228]\n");
  151.  
  152.     rez = pick_device(&devID, &HighRate);
  153.     if (devID >= 0) {
  154.         printf("Initializing devID %u\n",devID);
  155.         rez = init_device(devID);
  156.  
  157.         /*
  158.         The following load and play example source is coded inline here
  159.         to simply readability -- but it's so easy to add things that I just
  160.         kept adding stuff, so take it slow if you don't follow at first
  161.         */
  162.  
  163.         if (rez == 0) {
  164.  
  165.             /* load file and setup playback parameters */
  166.  
  167.             printf("\n MOD filename: ");
  168.             gets(filename);
  169.             printf("\n                      (5000-%u)",HighRate);
  170.             printf("\rPlayback rate: ");
  171.             SampleRate = atoi(_cgets(nums));
  172.             if (SampleRate < 5000)
  173.                 SampleRate = 5000;
  174.             if (SampleRate > HighRate)
  175.                 SampleRate = HighRate;
  176.  
  177.             LP.Func = LoadMod;
  178.             LP.FilenamePtr = filename;
  179.             LP.StartPos = 0L;           /* start at first byte */
  180.             LP.LoadSize = 0L;           /* autoload entire file */
  181.             LP.XMMflag = 0;             /* LP.XMMflag always=0 */
  182.             rez = RUCKDAC(&LP);
  183.             if (rez == 0) {
  184.  
  185.                /*
  186.                Increase SB Pro main and vol volumes to max (we bad now)
  187.                */
  188.  
  189.                if (devID == 5) {
  190.                   SPP.Func = SetVolMainSBP;
  191.                   SPP.Volume = 0x0F0F;
  192.                   rez2 = RUCKDAC(&SPP);
  193.                   SPP.Func = SetVolVocSBP;
  194.                   SPP.VolVoc = 0x0F0F;
  195.                   rez2 = RUCKDAC(&SPP);
  196.                }
  197.  
  198.                 /*
  199.                 set mod channel volumes to max
  200.                 */
  201.  
  202.                 SMP.VolCh1 = 255;
  203.                 SMP.VolCh2 = 255;
  204.                 SMP.VolCh3 = 255;
  205.                 SMP.VolCh4 = 255;
  206.                 SMP.Func = SetVolumeMod;
  207.                 rez2 = RUCKDAC(&SMP);  /* always error check! */
  208.  
  209.                 /*
  210.                 if SB Pro play in stereo
  211.                 */
  212.  
  213.                 if (devID < 5)
  214.                     SMP.Stereo = 0;
  215.                 else
  216.                     SMP.Stereo = 1;
  217.                 SMP.Func = SetStereoMod;
  218.                 rez2 = RUCKDAC(&SMP);
  219.  
  220.                 SMP.IntRate = SampleRate;
  221.  
  222.                 /*
  223.                 The SB Pro doubles the sample rate when doing stereo output
  224.                 so here we double the requested rate to rate needed by SBPro.
  225.                 This should be done _AFTER_ the SetStereoMod call above
  226.                 */
  227.  
  228.                 if (SMP.Stereo !=0)
  229.                     /* double rate if stereo */
  230.                     SMP.IntRate = SampleRate + SampleRate;
  231.  
  232.                 SMP.Func = SetIntRateMod;
  233.                 rez2 = RUCKDAC(&SMP);
  234.  
  235.                 /*
  236.                 SetFastMod can be used to play at higher rates than would
  237.                 otherwise be possible. The default is FastMod off and
  238.                 SliceAdj=1. On XTs, FastMod=1 may make the difference
  239.                 between being able to play mods at all. The SliceAdj is
  240.                 the number of bytes processed and stuffed into the DMA
  241.                 buffers per timer interrrupt. The default is typically
  242.                 sufficient to keep the DMA buffers full on fast machines,
  243.                 but on slower CPUs, a higher SliceAdj will make for a
  244.                 higher playback rate allowable. Actually, the code below
  245.                 doesn't change the defaults so I'll comment it out.
  246.                 Play around with the settings if you need to. (SliceAdj
  247.                 is relevant for DMA devices only, like the Sound Blasters.)
  248.                 */
  249.  
  250.                 SMP.Func = SetFastMod;
  251.                 SMP.FastMode = -1;      /* skip fastmode adjust */
  252.                 SMP.SliceAdj = 1;       /* default = 1, range 1-4096 */
  253.                 /* rez1 = RUCKDAC(&SMP) */
  254.  
  255.  
  256.                 /*
  257.                 if DMA device use DMA fore